// pages/modifyPassword/modifyPassword.js
var utils = require('../../utils/util.js');
var base64Js = require('../../utils/base64.js');
var md5Js = require('../../utils/md5.js');
var app = getApp();
Page({
  data:{},
  formSubmit:function(e){
      var req = e.detail.value;
      console.log(req);
      console.log(req["pwd"]);
      console.log(req.repwd);
      if(req["pwd"] != req.repwd ){
           wx.showModal({
            title:'提示',
            content:'两次密码输入不一样',
         })   
      }else{
        try{
            var _this = this;
            //获取缓存的用户对象信息
            var user_info =  wx.getStorageSync('user_info');
            //如果用户信息不为空则请求首页接口获取产品等信息。
            if(user_info){
              var request_data = user_info.user_itmes;
              var uid         = request_data.id.toString();
              var private_key = user_info.private_key;
              var t           =  new Date().getTime().toString();
              var str = 'newpassward='+req.repwd
              var payload = base64Js.encode(str);
              console.log(payload);
              var k           = md5Js.hex_md5(private_key+t+payload);//key
              var parms_url   = app.request_url+'UserPassward/?';
              parms_url += 'uid='+request_data.id;
              parms_url += '&userType='+request_data.type;
              parms_url += '&t='+t;
              parms_url += '&k='+k;
              parms_url += '&v='+app.version;;
              // parms_url = 'http://rest.test.freshqiao.com/UserPassward/?userType=1&t=1487055748582&k=d4d4de22dde489f876a77ea81eba0ca1&uid=100000202&v=2';            
              console.log(parms_url);
              //调用产品页接口
              wx.request({
                url: parms_url,
                data: {payload:payload},
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                method: 'POST',
                success: function(res){
                  var respones = res;
                  console.log(respones);
                  //请求成功分配模板变量
                  if((respones.statusCode=='200')&&(respones.data.Success==1)){
                    console.log(1111);
                      wx.showToast({
                        title: '修改成功',
                     });
                      setTimeout(function(){
                        wx.clearStorageSync();
                        wx.clearStorage({
                          key: 'user_info',
                        })
                        wx.redirectTo({
                          url: '../user/user',
                        })
                      },2000) 
                  }
                }
              })
            }
        }catch(e){
          console.log('error=>'+e.message);return false;
        }
      }
    },

  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})